<template>
    <div class="login-wrap">
    <div class="ms-login">
        <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
            <div class="v-title">账号</div>
            <el-form-item prop="username">
                <el-input v-model="param.username" placeholder="请输入账号"/>
            </el-form-item>
            <div class="v-title">密码</div>
            <el-form-item prop="password">
                <el-input type="password" v-model="param.password" @keyup.enter.native="submitForm()" placeholder="请输入密码"/>
            </el-form-item>
            <div style="width:100%;display: flex;flex-direction: row;justify-content: space-between">
                <el-checkbox v-model="rememberMe" style="margin:0px 0px 20px 0px;">记住密码</el-checkbox>
                <div class="v-forget-password">
                    忘记密码
                </div>
            </div>
            <div class="login-btn">
                <el-button type="primary" style="background: #409EFF" @click="submitForm()">
                    <span v-if="!loading">登 录</span>
                    <span v-else>登 录 中...</span>
                </el-button>
            </div>
        </el-form>
    </div>
</div>
</template>
<script>
    import { postLogin,getRoleList } from '@/api/login'
    import { setToken } from '@/utils/auth'
    export default {
        data() {
            return {
                loading: false,
                rememberMe: true,
                param: {
                    username: "",
                    password: "",
                },
                rules: {
                    username: [
                        {
                            required: true,
                            message: "请输入账号",
                            trigger: "blur",
                        },
                    ],
                    password: [
                        {
                            required: true,
                            message: "请输入密码",
                            trigger: "blur",
                        },
                    ],
                },
            };
        },
        created() {
            if (localStorage.getItem("username")) {
                this.param.username = localStorage.getItem("username");
            } else {
                this.param.username = "";
            }
            if (localStorage.getItem("password")) {
                this.param.password = localStorage.getItem("password");
            } else {
                this.param.password = "";
            }
        },
        computed: {},
        methods: {
            // 点击登录按钮
            submitForm() {
                this.$refs.login.validate(valid => {
                    if (valid) {
                        this.loading = true;
                        postLogin(this.param).then((res) => {
                            console.log("登录信息：",res)
                            this.loading = false
                            if (res.code == 200) {
                                setToken(res.token);
                                localStorage.setItem("areaId",res.areaId)
                                localStorage.setItem("areaName",res.areaName)
                                this.getRoleInfo();
                            } else {
                                this.$message.error(es.data.msg)
                            }
                        }).catch(() => {
                            this.loading = false
                            // this.$message.error('数据异常，请联系管理员')
                        })
                    }
                });
            },
            getRoleInfo(){
                getRoleList({}).then((res) => {
                    console.log(res)
                    if (res.code == 200){
                        let roleList = [];
                        res.data.map(item=>{
                            roleList.push({
                                roleId:item.roleId,
                                roleKey:item.roleKey,
                                roleName:item.roleName,
                            })
                        })
                        localStorage.setItem("roleList",JSON.stringify(roleList));
                        this.$router.push("/home");
                    }
                });
            },
        },
    };
</script>
<style scoped lang="scss">

    .yzm-view{
        height: 40px;
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        line-height: 40px;
    }

    .login-wrap {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        background-image: url("../../assets/images/login-bg.jpg");
        background-size: 100% 100%;
    }

    .input-icon {
        height: 10px;
        width: 5px;
        margin-left: 2px;
    }

    .left-logo {
        width: 200px;
        margin-left: 40px;
        margin-top: 40px;
    }

    .ms-title {
        text-align: center;
        font-size: 22px;

        .logo-img {
            width: 180px;
            margin: 30px 0px 25px 0px;
        }
    }

    .ms-login {
        width: 34%;
        border-radius: 5px;
        background: rgba(255, 255, 255, 0.7);
        overflow: hidden;
        box-shadow: 1px 1px 15px rgba(94, 96, 129, 0.5);
    }

    .v-title{
        margin-bottom: 5px;
        font-weight: bold;
        font-size: 20px
    }

    .v-forget-password{
        margin-bottom: 5px;
        font-weight: bold;
        font-size: 18px;
        color: #0A77F9;
    }

    /deep/ .el-checkbox__label {
        font-size: 17px !important;
    }

    .ms-content {
        padding:12%;
    }

    .login-btn {
        text-align: center;
    }

    .login-btn button {
        width: 100%;
        height: 40px;
        margin-bottom: 10px;
    }

    .login-tips {
        font-size: 12px;
        line-height: 30px;
        color: #333;
    }

    .el-login-footer {
        margin: 20px 0px;
        height: 40px;
        width: 100%;
        text-align: center;
        font-family: Arial;
        font-size: 12px;
        letter-spacing: 1px;
    }

    .el-login-footer div {
        padding-bottom: 10px;
    }

    .el-login-footer div span {
        margin-right: 20px;
    }

    ::v-deep .el-input--medium .el-input__inner {
        height: 40px;
        line-height: 40px;
    }

</style>
